# -*- encoding:utf-8 -*-
'''
	usedao.py
	======================
	@descript: 
	@copyright:chinasoft
	@author:cjgong
	@date: 2021-03-04 10:02
	@version:v1.0
'''
from app01.pymysqlutil import PyMysqlUtil
from app01.user import User, UserPage
from log.common import get_logger

class UserDao:
    v_log = get_logger('UserDao')
    @staticmethod
    def f_select_user():
        UserDao.v_log.debug('f_select_user开始执行……')
        v_result = []
        var_psyMysqlUtil = PyMysqlUtil()
        with var_psyMysqlUtil  as  var_cursor:
            var_sql = 'SELECT django02.`user_id`,django02.`user_name`,django02.`user_sex`,django02.`user_height`,django02.`tel` FROM django02;'
            var_cursor.execute(var_sql)
            var_resultset02 = var_cursor.fetchall()
            for element in var_resultset02:
                # print(element)
                from app01.user import User
                v_user = User(**element)
                v_result.append(v_user)
                pass
            pass
        UserDao.v_log.debug('f_select_user执行结束……')
        return v_result

    @classmethod
    def f_delete_user(cls, p_user_id):
        UserDao.v_log.debug('f_delete_user开始执行……')
        var_psyMysqlUtil = PyMysqlUtil()
        with var_psyMysqlUtil  as  var_cursor:
            var_sql = 'DELETE FROM django02 WHERE django02.`user_id`=%s;'
            var_cursor.execute(var_sql, (p_user_id,))
            pass
        UserDao.v_log.debug('f_delete_user执行结束……')

    @classmethod
    def f_insert_user(cls, p_user: User):
        UserDao.v_log.debug('f_insert_user开始执行……')
        var_psyMysqlUtil = PyMysqlUtil()
        with var_psyMysqlUtil  as  var_cursor:
            var_sql = 'INSERT INTO django02(django02.`user_name`,django02.`user_sex`,django02.`user_height`,django02.`tel`) VALUE(%s,%s,%s,%s);'
            var_cursor.execute(var_sql, (p_user.v_user_name, p_user.v_user_sex, p_user.v_user_height, p_user.v_tel))
        pass
        UserDao.v_log.debug('f_insert_user执行结束……')


    pass

    @classmethod
    def f_select_user_id(cls, v_user_id):
        UserDao.v_log.debug('f_select_user_id开始执行……')
        v_result = None
        var_psyMysqlUtil = PyMysqlUtil()
        with var_psyMysqlUtil  as  var_cursor:
            var_sql = 'SELECT django02.`user_id`,django02.`user_name`,django02.`user_sex`,django02.`user_height`,django02.`tel` FROM django02 WHERE django02.`user_id`=%s'
            var_cursor.execute(var_sql, (v_user_id,))
            var_resultset02 = var_cursor.fetchone()
            v_result = User(**var_resultset02 )
            # user_name = 'user_name', user_sex = 'user_sex', user_height = 'user_height', tel = 'tel'
            pass
        UserDao.v_log.debug('f_select_user_id执行结束……')
        return v_result

    @classmethod
    def f_update_user(cls, p_user: User):
        UserDao.v_log.debug('f_update_user开始执行……')
        var_psyMysqlUtil = PyMysqlUtil()
        with var_psyMysqlUtil  as  var_cursor:
            var_sql = 'UPDATE django02 SET django02.`user_name`=%s,django02.`user_sex`=%s,django02.`user_height`=%s,django02.`tel`=%s WHERE django02.`user_id`=%s;'
            var_cursor.execute(var_sql, (
            p_user.v_user_name, p_user.v_user_sex, p_user.v_user_height, p_user.v_tel, p_user.v_user_id))
            pass
        UserDao.v_log.debug('f_update_user执行结束……')

    # 设置p_Userpage里的分页里显示的记录数
    @classmethod
    def f_select_user_page(cls, p_Userpage: UserPage):
        UserDao.v_log.debug('f_select_user_page开始执行……')
        v_user_list = []
        var_psyMysqlUtil = PyMysqlUtil()
        with var_psyMysqlUtil  as  var_cursor:
            var_sql = 'SELECT django02.`user_id`,django02.`user_name`,django02.`user_sex`,django02.`user_height`,django02.`tel` FROM  django02 LIMIT %s,%s'
            v_01 = (p_Userpage.v_currentpage - 1) * p_Userpage.v_pageunit
            v_02 = p_Userpage.v_pageunit
            var_cursor.execute(var_sql, (v_01, v_02))
            var_resultset02 = var_cursor.fetchall()
            for element in var_resultset02:
                v_user = User(**element)
                v_user_list.append(v_user)
                pass
            pass
        p_Userpage.v_user_list = v_user_list
        UserDao.v_log.debug('f_select_user_page执行结束……')
        return p_Userpage
    # 设置p_Userpage总记录数的值
    @classmethod
    def f_select_totalrecord(cls, p_Userpage: UserPage):
        UserDao.v_log.debug('f_select_totalrecord开始执行……')
        var_psyMysqlUtil = PyMysqlUtil()
        with var_psyMysqlUtil  as  var_cursor:
            var_sql = 'SELECT COUNT(*) AS totalrecord FROM django02'
            var_cursor.execute(var_sql)
            var_resultset02 = var_cursor.fetchall()
            for element in var_resultset02:
                v_total_record = element.get('totalrecord', None)
                p_Userpage.v_totalrecord = v_total_record
                pass
            pass
        UserDao.v_log.debug('f_select_totalrecord执行结束……')
        return p_Userpage

    pass